require"common"

local nQxcMapping =
{  -1, -1, -1,  6, -1, -1,  6,  5, -1, -1, -1,  6,  6,  6,  5,  4, -1, -1, -1,  6, -1, -1,
	6,  5,  6,  6,  6,  6,  5,  5,  4,  3, -1, -1, -1,  6, -1, -1,  6,  5, -1, -1, -1,  6,
	6,  6,	5,  4,  6,  6,  6,  6, 6,   6,  6,  5,  5,  5,  5,  5,  4,  4,  3,  2, -1, -1,
   -1,  6, -1, -1,  6,  5, -1, -1, -1,  6,  6,  6,  5,  4, -1, -1, -1,  6, -1, -1,  6,  5,
	6,  6,  6,  6,  5,  5,  4,  3,  6,  6,  6,  6,  6,  6,  6,  5,  6,  6,  6,  6,  6,  6,
	5,  4,  5,  5,  5,  5,  5,  5,  5,  5,  4,  4,	4,  4,  3,  3,  2,  1 ,}
--判断字符串
function judge_str( num , max)
        local out = ""
        local st = {}
        if num == nil then return nil end
        local len = string.len( num )

        for i=1,len,1 do
                c = string.sub(num,i,i)
                if tonumber(c) > max or tonumber(c) < 0 then return nil end

        end
        return true
end

--数字字符串排序

function sort_str_pl5( num )
	num = string.gsub( num, "*", "")
	local out = ""
	local st = {}
	local len = string.len( num )
	for i=1,len do
		table.insert( st, string.sub(num,i,i) )
	end
	table.sort(st)
	for i=1,len do
		out = out..st[i]
	end

	return out

end
--获取字符串中唯一的字符个数

function get_distinct_num( num )
	local dist = {}
	local str = {}
	local out = ""
	local len = string.len( num )
	str = sort_str_pl5(num)
	for i=1,len do
		c = string.sub(str,i,i)
		if not in_table( c, dist ) then
			table.insert( dist, c )
		end
	end
	for i = 1,table.getn(dist) do
		out = out..dist[i]
	end
	return	out , table.getn(dist)

end
--============================================================================================
function ComputeBingoNumQxc(jItemstr,djStr,butType)
	local bingoNum ={0,0,0,0,0,0,0}
	local bullStr = {}
	local queryStr ={}

	for i = 1,7 do
		bullStr[i] = string.sub(djStr,i,i)
	end
	if butType == '1'then
		for i = 1, 7 do
			queryStr[i] = string.sub(jItemstr,i,i)
		end
		for v, k in ipairs(bullStr) do
			if k == queryStr[v] then
				bingoNum[v] = 1
			else
				bingoNum[v] = 0
			end
		end
	elseif butType == '2' then
		jItem = Split(jItemstr,"#")
		for i = 1, 7 do
			queryStr[i] = {}
			for j = 1 ,string.len(jItem[i]) do
				queryStr[i][j] = string.sub(jItem[i],j,j)
			end
		end
		for i = 1, 7 do
			for _,k in ipairs(queryStr[i]) do
				if k == bullStr[i]  then
					bingoNum[i] = 1
				end
			end
		end
	end
	return bingoNum
end
--===========================================================================================
function ComputeQxc( nMaxNum,  nBingoNum)

	local i;
	local nIndex ={}
	local nTemp = {}
	local nResult;
	local nBingo = {0,0,0,0,0,0,0}

	for i = 1, 7 do
		if (nBingoNum[i] > 1 or nMaxNum[i] < nBingoNum[i] or nBingoNum[i] > 1 or nBingoNum[i] < 0) then
			return nBingo
		end
	end
	for i0 = 1, nBingoNum[1]+1 do

		if i0 == 1 then
			nTemp[1] = combination(nMaxNum[1] - nBingoNum[1], 1);
		else
			nTemp[1] = nBingoNum[1]
		end
		if nTemp[1] ~= 0 then
			for i1 = 1, nBingoNum[2]+1 do
				if  i1 == 1 then
					nTemp[2] = combination(nMaxNum[2] - nBingoNum[2], 1)
				else
					nTemp[2] = nBingoNum[2]
				end
				if nTemp[2] ~= 0 then
					for i2 = 1, nBingoNum[3]+1 do

						if i2 == 1 then
							nTemp[3] = combination(nMaxNum[3] - nBingoNum[3], 1)
						else
							nTemp[3] = nBingoNum[3]
						end
						if nTemp[3] ~= 0 then
							for i3 = 1, nBingoNum[4]+1 do

								if i3 == 1 then
									nTemp[4] = combination(nMaxNum[4] - nBingoNum[4], 1)
								else
									nTemp[4] = nBingoNum[4]
								end
								if nTemp[4] ~= 0 then
									for i4 = 1, nBingoNum[5]+1 do

										if i4 == 1 then
											nTemp[5] = combination(nMaxNum[5] - nBingoNum[5], 1);
										else
											nTemp[5] = nBingoNum[5]
										end
										if nTemp[5] ~= 0 then
											for i5 = 1, nBingoNum[6]+1 do

												if i5 == 1 then
													nTemp[6] = combination(nMaxNum[6] - nBingoNum[6], 1);
												else
													nTemp[6] = nBingoNum[6]
												end
												if nTemp[6] ~= 0 then
													for i6 = 1, nBingoNum[7]+1 do

														if i6 == 1 then
															nTemp[7] = combination(nMaxNum[7] - nBingoNum[7], 1)
														else
															nTemp[7] = nBingoNum[7]
														end
														if nTemp[7] ~= 0 then
															nResult = 1
															for i = 1, 7 do
																nResult = nResult * nTemp[i];
															end
															--j = nQxcMapping[power(2,6)*(i6-1) + power(2,5)*(i5-1) + power(2,4)*(i4-1) + power(2,3)*(i3-1) + power(2,2)*(i2-1) + power(2,1)*(i1-1) + power(2,0)*(i0-1) + 1]
															j = nQxcMapping[power(2,6)*(i0-1) + power(2,5)*(i1-1) + power(2,4)*(i2-1) + power(2,3)*(i3-1) + power(2,2)*(i4-1) + power(2,1)*(i5-1) + power(2,0)*(i6-1) + 1]
															if j ~= -1 then
																nBingo[j] = nBingo[j] + nResult;
															end
														end
													end
												end
											end
										end
									end
								end
							end
						end
					end
				end
			end
		end
	end
	return nBingo
end
--返回注数,倍投数 , 投注串, 指令串
function random_qxc( butType, inStr )
	local butNum = 1
	local butPower = 1
	local zlStr = ""
	local butStr = ""
	local itemList = {}
	local itemLen = 0;
	itemList = Split( inStr, "#B" )
	itemLen = table.getn( itemList )
	qjStr = string.gsub( inStr, "#B", "0")
	if itemLen > 2 or itemLen < 1 or string.find(qjStr,"%D") then return 0 end

	if tonumber( itemList[2] ) then butPower = tonumber( itemList[2] ) else butPower = 1 end
	if butPower >99 or butPower < 1 then return 0 end
	--单式机选  7#注数
	if butType == '1' then
		butNum = tonumber(itemList[1])
		for i=1,butNum do
			local nums = selectNumber(7,0,1,2,3,4,5,6,7,8,9)
			zlStr = zlStr..nums[1]..nums[2]..nums[3]..nums[4]..nums[5]..nums[6]..nums[7]
			if i<butNum then zlStr = zlStr.."#" end
		end
		zlStr = zlStr.."#B"..butPower
		butNum,butPower,butStr = check_qxc('1', zlStr)
	end
	return butNum, butPower, butStr, zlStr
end

--返回注数,倍投数,投注串
function check_qxc( butType, inStr)
	local qxStr;
	local qjStr;
	local butNum = 0;
	local butPower = 1;
	local butStr = "";
	local itemList = {}
	local itemLen = 0;
	itemList = Split( inStr, "#B" )
	itemLen = table.getn( itemList )

	qjStr = string.gsub( inStr, "#B", "0")
	qxStr = string.gsub( qjStr, "#", "0")
	if itemLen > 2 or itemLen < 1 or string.find(qxStr,"%D") then return 0 end

	if tonumber( itemList[2] ) then butPower = tonumber( itemList[2] ) else butPower = 1 end
	if butPower >99 or butPower < 1 then return 0 end

	--单式自选 71#注码  71#1234567#7654321 表示自选2注
	if  butType == '1' then
		xItem = Split(itemList[1], "#")
		butNum = #xItem
		if butNum > 5 or butNum < 1  then
			return 0
		end
		for i=1,#xItem do
			if string.len(xItem[i]) ~= 7  then return 0 end
			if  not judge_str(xItem[i],9) then return 0 end
		end
	--复式自选  73#第1位#第2位#第3位#第4位#第5位#第6位#第7位
	elseif butType == '2' then
		xItem = Split(itemList[1], "#")
		if #xItem ~= 7  then return 0 end
		for i=1,#xItem  do
			_,distNum = get_distinct_num(xItem[i])
			if distNum ~= string.len(xItem[i]) then return 0 end
			if  not judge_str(xItem[i],9) then return 0 end
		end
		butNum = string.len(xItem[1])*string.len(xItem[2])*string.len(xItem[3])*string.len(xItem[4])*string.len(xItem[5])*string.len(xItem[6])*string.len(xItem[7])
	else
		return 0
	end
	return butNum,butPower,inStr,inStr
end

function query_qxc(butType,queryStr,djStr)
	local nBingo = {0,0,0,0,0,0}
	local butPower = 1
	local itemList = {}
	local bullStr = {}
	local bingoNum = {}
	local nMaxNum = {}
	local temp = {}
	itemList = Split( queryStr, "#B" )
	if tonumber( itemList[2] ) then butPower = tonumber( itemList[2] ) else butPower = 1 end

	if butType == '1' then
		jItem = Split(itemList[1],"#")
		nMaxNum = {1,1,1,1,1,1,1}
		for i = 1 ,table.getn(jItem) do
			bingoNum[i] = {}
			bingoNum[i] = ComputeBingoNumQxc(jItem[i],djStr,butType)
			temp = nBingo
			nBingo = ComputeQxc(nMaxNum,bingoNum[i])
			for j = 1, 6 do
				nBingo[j] = nBingo[j] + temp[j]
			end
		end
		for i = 1 ,6 do
			nBingo[i] = nBingo[i] * butPower
		end
	elseif butType == '2' then
		jItem = Split(itemList[1],"#")
		for i = 1, 7 do
			nMaxNum[i] = string.len(jItem[i])
		end
		bingoNum = ComputeBingoNumQxc(queryStr,djStr,butType)
		nBingo = ComputeQxc(nMaxNum,bingoNum)
		for i = 1 ,6 do
			nBingo[i] = nBingo[i] * butPower
		end
	end
	return nBingo[1]..","..nBingo[2]..","..nBingo[3]..","..nBingo[4]..","..nBingo[5]..","..nBingo[6]
end
--print(random_qxc('1',"5"))
--print(check_qxc('1',"1234567#B10"))
--print(check_qxc('2',"0#1#2#3#4#5#678#B10"))
--print(ComputeQxc({10,10,10,10,10,1,1},{1,1,1,1,1,1,1}))
--print(query_qxc('1',"1234567#1254567#B10","1234567"))
--print(query_qxc('2',"8#3#7#0#1#3#0#B99","8746834"))

